import {
    applyClaim,
    approvalClaim,
    claimRevenue,
    createRevenue,
    deleteRevenueById,
    getListByBevenueId,
    getListForPage,
    getRevenueById,
    getUnClaimForPage,
    unClaimRevenue,
    updateRevenue,
    batchCancelClaim,
    batchSubmitClaim,
    batchClaimRevenue
} from '../../services/capital/revenue';
import { message } from 'antd';
import Constants from '../../utils/Constants'

const R = require('ramda');

export default {
    namespace: 'revenue',
    state: {
        page: {},
        dataSource: {},
        list: [],
        claimedPage: {}, //来账已认领费用page
        claimedList: [], //来账已认领费用list
        unClaimPage: {}, //可认领的费用page
        unClaimList: [], //可认领的费用list
        approvalPage: {}, //本次审核的费用page
        approvalList: [] //本次审核的费用list
    },
    reducers: {
        /**
         * 刷新state
         *
         * @param state
         */
        refreshState(state, { payload }) {
            return { ...state, ...payload };
        }
    },
    effects: {
        /**
         * 分页查询来账
         *
         * @param call
         * @param put
         *
         */
            *getListForPage({ payload }, { call, put }) {
            const res = yield call(getListForPage, { ...R.omit(['onSuccess', 'onFail'])(payload) });
            if (res && res.data.resultCode === "ok") {
                yield put({
                    type: 'refreshState',
                    payload: {
                        list: res.data.content.list,
                        page: res.data.content.page
                    }
                });
            } else {
                if (payload.onFail) payload.onFail();
            }
        },
        /**
         * 新增来账
         *
         * @param call
         */
            *createRevenue({ payload }, { call }) {
            const res = yield call(createRevenue, { ...R.omit(['onSuccess', 'onFail'])(payload) });
            if (res && res.data.resultCode === "ok") {
                message.info("新增成功！");
                if (payload.onSuccess) payload.onSuccess(res.data.content);
            } else {
                if (payload.onFail) payload.onFail();
            }
        },
        /**
         * 更新来账
         *
         * @param call
         * @param put
         * @param select
         */
            *updateRevenue({ payload }, { call, put }) {
            const res = yield call(updateRevenue, { ...R.omit(['onSuccess', 'onFail'])(payload) });
            if (res && res.data.resultCode === "ok") {
                message.info("更新成功！");
                if (payload.onSuccess) payload.onSuccess(res.data.content);
            } else {
                if (payload.onFail) payload.onFail();
            }
        },
        /**
         * 根据id删除来账
         *
         * @param id
         */
            *deleteRevenueById({ payload }, { call }) {
            const res = yield call(deleteRevenueById, { ...R.omit(['onSuccess', 'onFail'])(payload) });
            if (res && res.data.resultCode === "ok") {
                message.info("删除成功！");
                if (payload.onSuccess) payload.onSuccess();
            } else {
                if (payload.onFail) payload.onFail();
            }
        },
        /**
         * 根据id获取来账
         *
         * @param id
         */
            *getRevenueById({ payload }, { call, put }) {
            if (payload.id) {
                const res = yield call(getRevenueById, { ...R.omit(['onSuccess', 'onFail'])(payload) });
                if (res && res.data.resultCode === "ok") {
                    yield put({
                        type: 'refreshState',
                        payload: {
                            dataSource: res.data.content
                        }
                    });
                    if (payload.onSuccess) payload.onSuccess();
                } else {
                    if (payload.onFail) payload.onFail();
                }
            } else {
                yield put({
                    type: 'refreshState',
                    payload: {
                        dataSource: {}
                    }
                });
            }
        },
        /**
         * 来账认领
         *
         */
            *claimRevenue({ payload }, { call, put }) {
            const res = yield call(claimRevenue, { ...R.omit(['onSuccess', 'onFail'])(payload) });
            if (res && res.data.resultCode === "ok") {
                message.info("认领成功！");
                if (payload.onSuccess) payload.onSuccess();
            } else {
                if (payload.onFail) payload.onFail();
            }
        },

        /**
         * 批量来账认领
         *
         */
            *batchClaimRevenue({ payload }, { call, put }) {
            const res = yield call(batchClaimRevenue, { ...R.omit(['onSuccess', 'onFail'])(payload) });
            if (res && res.data.resultCode === "ok") {
                message.info("认领成功！");
                if (payload.onSuccess) payload.onSuccess();
            } else {
                if (payload.onFail) payload.onFail();
            }
        },

        /**
         * 取消来账认领
         *
         * @param orderId
         * @param amount
         * @return
         */
            *unClaimRevenue({ payload }, { call, put }) {
            const res = yield call(unClaimRevenue, { ...R.omit(['onSuccess', 'onFail'])(payload) });
            if (res && res.data.resultCode === "ok") {
                message.info("取消认领成功！");
                if (payload.onSuccess) payload.onSuccess();
            } else {
                if (payload.onFail) payload.onFail();
            }
        },
        /**
         * 根据来账id查询已认领的费用
         *
         * @param claimId
         * @return
         */
            *getListByBevenueId({ payload }, { call, put }) {
            const res = yield call(getListByBevenueId, { ...R.omit(['onSuccess', 'onFail'])(payload) });
            if (res && res.data.resultCode === "ok") {
                yield put({
                    type: 'refreshState',
                    payload: {
                        [payload.approval ? 'approvalList' : 'claimedList']: res.data.content.list ? res.data.content.list : [],
                        [payload.approval ? 'approvalPage' : 'claimedPage']: res.data.content.page ? res.data.content.page : Constants.DEFAULT_PAGE
                    }
                });
                if (payload.onSuccess) payload.onSuccess();
            } else {
                if (payload.onFail) payload.onFail();
            }
        },

        /**
         *费用认领状态批量修改 包括：1.财务审核通过 2.财务退回
         *
         * @param revenueId
         * @param status
         * @param memo
         * @return
         */
            *approvalClaim({ payload }, { call, put }) {

            const res = yield call(approvalClaim, { ...R.omit(['onSuccess', 'onFail'])(payload) });
            if (res && res.data.resultCode === "ok") {
                message.info("审核结果提交成功！");
                if (payload.onSuccess) payload.onSuccess();
            } else {
                if (payload.onFail) payload.onFail();
            }
        },



        /**
         *根据订单号、客户名称模糊查询还有余额未认领完的费用
         *
         * @param orderNo
         * @param cstName
         * @param page
         * @return
         */
            *getUnClaimForPage({ payload }, { call, put }) {
            const res = yield call(getUnClaimForPage, { ...R.omit(['onSuccess', 'onFail'])(payload) });
            if (res && res.data.resultCode === "ok") {
                yield put({
                    type: 'refreshState',
                    payload: {
                        unClaimList: res.data.content.list ? res.data.content.list : [],
                        unClaimPage: res.data.content.page ? res.data.content.page : Constants.DEFAULT_PAGE
                    }
                });
                if (payload.onSuccess) payload.onSuccess();
            } else {
                if (payload.onFail) payload.onFail();
            }
        },

        /**
         *单条认领记录提交审核
         *
         * @param claimId
         * @return
         */
            *applyClaim({ payload }, { call, put }) {
            const res = yield call(applyClaim, { ...R.omit(['onSuccess', 'onFail'])(payload) });
            if (res && res.data.resultCode === "ok") {
                message.info("当前认领提交财务审核成功！");
                if (payload.onSuccess) payload.onSuccess();
            } else {
                if (payload.onFail) payload.onFail();
            }
        },

        *batchCancelClaim({ payload }, { call, put, select }) {
            const res = yield call(batchCancelClaim, { ...R.omit(['onSuccess', 'onFail'])(payload) });
            if (res && res.data.resultCode === "ok") {
                message.info("批量取消成功");
                if (payload.onSuccess) payload.onSuccess();
            } else {
                if (payload.onFail) payload.onFail();
            }
        },

        *batchSubmitClaim({ payload }, { call, put, select }) {
            const res = yield call(batchSubmitClaim, { ...R.omit(['onSuccess', 'onFail'])(payload) });
            if (res && res.data.resultCode === "ok") {
                message.info("批量提交财务审核成功");
                if (payload.onSuccess) payload.onSuccess();
            } else {
                if (payload.onFail) payload.onFail();
            }
        },

    },
    subscriptions: {
        setup({ history, dispatch }) {
            return history.listen(({ pathname }) => {
                const menus = JSON.parse(sessionStorage.getItem(Constants.USER_MENU));
                if (menus && !menus.find(item => item.includes(pathname)) && pathname != '/logout') {
                    document.location.href = "/notAuthorized"
                }
            });
        }
    }
};
